package com.lixianhe.onlineexam.controller;

import com.lixianhe.onlineexam.pojo.Answer;
import com.lixianhe.onlineexam.pojo.FillQuestion;
import com.lixianhe.onlineexam.pojo.JudgeQuestion;
import com.lixianhe.onlineexam.pojo.MultiQuestion;
import com.lixianhe.onlineexam.service.FillQuestionService;
import com.lixianhe.onlineexam.service.JudgeQuestionService;
import com.lixianhe.onlineexam.service.MultiQuestionService;

import com.lixianhe.onlineexam.service.AnswerService;
import com.lixianhe.onlineexam.util.ApiResultHandler;
import com.lixianhe.onlineexam.util.PageBean;
import com.lixianhe.onlineexam.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class AnswerController {
    @Autowired
    private AnswerService answerService;
    @Autowired
    private MultiQuestionService multiQuestionService;
    @Autowired
    private FillQuestionService fillQuestionService;
    @Autowired
    private JudgeQuestionService judgeQuestionService;

    @GetMapping("/answers/{page}/{size}")
    public R answers(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        PageBean<Answer> answerPageInfo = answerService.selectAnswerByPage(page, size);
        return ApiResultHandler.buildApiResult(200, "所有数据", answerPageInfo);
    }

    @GetMapping("question/{questionId}/{type}")
    public R selectQuestion(@PathVariable("questionId") Integer questionId, @PathVariable("type") String type) {
        return answerService.selectQuestion(questionId, type);
    }

    @PutMapping("questionMulti")
    public R updateQuestionMulti(@RequestBody MultiQuestion multiQuestion) {
        int res = multiQuestionService.updateQuestion(multiQuestion);
        if (res == 1) return ApiResultHandler.buildApiResult(200, "更新选择题成功", res);
        return ApiResultHandler.buildApiResult(400, "更新选择题成功", res);
    }

    @PutMapping("questionFill")
    public R updateQuestionFill(@RequestBody FillQuestion fillQuestion) {
        int res = fillQuestionService.updateQuestion(fillQuestion);
        if (res == 1) return ApiResultHandler.buildApiResult(200, "更新填空题成功", res);
        return ApiResultHandler.buildApiResult(400, "更新填空题成功", res);
    }

    @PutMapping("questionJudge")
    public R updateQuestionJudge(@RequestBody JudgeQuestion judgeQuestion) {
        int res = judgeQuestionService.updateQuestion(judgeQuestion);
        if (res == 1) return ApiResultHandler.buildApiResult(200, "更新判断题成功", res);
        return ApiResultHandler.buildApiResult(400, "更新判断题成功", res);
    }

    @DeleteMapping("question/{questionId}/{type}")
    public R deleteQuestion(@PathVariable("questionId") Integer questionId, @PathVariable("type") String type) {
        return answerService.deleteQuestion(questionId, type);
    }
}
